//动态数组vector的应用
#include <iostream>
#include <vector> 
#include <fstream>
#include <cstring>
using namespace std;

int main(int argc, char const *argv[])
{
    fstream file("英雄名单.txt",ios::in);

    //动态数组
    vector<string>heros;

    char name[50];
    for (int i = 0;!file.eof() ; i++)
    {
        file.getline(name,50);
        heros.push_back(name);

        cout << "capacity:" << heros.capacity() << endl;
        cout << "size:" << heros.size() << endl;
    }

    heros.shrink_to_fit();  //压缩节省空间

    //使用各种方式输出名单

    //1.传统方式
    cout << "英雄名单:" << endl;
    for(int i = 0; i < heros.size()&&!heros[i].empty(); i++)
    {
        cout << heros[i] << endl;
    }
    cout << endl;

    //2.枚举方式
    cout << "英雄名单:" << endl;
    for (string hero: heros)
    {
        if(hero.empty())
            break;
        cout << hero << endl;
    }
    cout << endl;

    //3.迭代器方式
    cout << "英雄名单:" << endl;
    for (vector<string>::iterator it = heros.begin();it!=heros.end();it++)
    {
        if((*it).empty())
            break;
        cout << *it << endl;
    }
    cout << endl;

    return 0;

}
